package andriod.action.flow;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OFlowFormProporty;
import com.jcrj.entity.OFormDefinition;
import com.jcrj.entity.OLeave;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkflow;
import com.jcrj.service.FlowFormService;
import com.jcrj.service.HolidayService;
import com.jcrj.service.WorkflowManagerService;
import com.jcrj.util.DateUtil;

public class FlowFromAction extends BaseAction{
	private FlowFormService flowFormService;
	private OWorkflow workflow;
	private int flowId;
	private OFormDefinition definition;
	private WorkflowManagerService workflowManagerService;

	public OWorkflow getWorkflow() {
		return workflow;
	}

	public void setWorkflow(OWorkflow workflow) {
		this.workflow = workflow;
	}

	public FlowFormService getFlowFormService() {
		return flowFormService;
	}

	public void setFlowFormService(FlowFormService flowFormService) {
		this.flowFormService = flowFormService;
	}

	public int getFlowId() {
		return flowId;
	}

	public void setFlowId(int flowId) {
		this.flowId = flowId;
	}

	public OFormDefinition getDefinition() {
		return definition;
	}

	public void setDefinition(OFormDefinition definition) {
		this.definition = definition;
	}

	public WorkflowManagerService getWorkflowManagerService() {
		return workflowManagerService;
	}

	public void setWorkflowManagerService(
			WorkflowManagerService workflowManagerService) {
		this.workflowManagerService = workflowManagerService;
	}

	public String getFlowFromProperty() {
		this.workflow = this.workflowManagerService.getWorkflow(flowId);
		this.definition = this.flowFormService.getFormDefinition(this.workflow
				.getFormId().intValue());
		String content = definition.getContent();
		List<String> ids = getIds(content);
		List<OFlowFormProporty> list = this.flowFormService.flowFormProportyList(definition.getId());
		Map<String, OFlowFormProporty> map = new HashMap<String, OFlowFormProporty>();
		for(OFlowFormProporty formProporty:list){
			map.put(formProporty.getColumnName(), formProporty);
		}
		List<OFlowFormProporty> resultList = new ArrayList<OFlowFormProporty>();
		for(String id:ids){
			resultList.add(map.get(id));
		}
		JSONArray jsonArray = JSONArray.fromObject(resultList);
		getResponse().setContentType("text/json;charset=utf-8");
		try {
			PrintWriter writer = getResponse().getWriter();
			writer.print(jsonArray.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return null;
	}
	private static List<String> getIds(String content){
		List<String> list =  new ArrayList<String>();
		Pattern pattern = Pattern.compile("id\\s*=\\s*\"jcoa[^\"]+"+"\"");
		System.out.println(pattern.pattern());
		Matcher matcher = pattern.matcher(content);
		while(matcher.find()){
			String str = matcher.group();
			list.add(str.split("\"")[1]);
		}
		return list;
	}
	
	

}
